package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerfilestandardcolumnDAO")
@Transactional
public class CrawlerfilestandardcolumnDAOImpl extends AbstractJpaDao<Crawlerfilestandardcolumn>
		implements CrawlerfilestandardcolumnDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfilestandardcolumn.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerfilestandardcolumnDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnById(Integer id) throws DataAccessException {

		return findCrawlerfilestandardcolumnById(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardcolumnById", startResult, maxRows, id);
			return (Crawlerfilestandardcolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByName(String name) throws DataAccessException {

		return findCrawlerfilestandardcolumnByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardcolumnByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandardcolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByNameContaining(String name)
			throws DataAccessException {

		return findCrawlerfilestandardcolumnByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByNameContaining(String name, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardcolumnByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestandardcolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByDescription(String description)
			throws DataAccessException {

		return findCrawlerfilestandardcolumnByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByDescription(String description,
			int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardcolumnByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfilestandardcolumn>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerfilestandardcolumnByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByDescriptionContaining(String description,
			int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestandardcolumnByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerfilestandardcolumn>(query.getResultList());
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfilestandardcolumnByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardcolumnByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfilestandardcolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnsByIds(Set<Integer> ids)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardcolumnsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findAllCrawlerfilestandardcolumns() throws DataAccessException {

		return findAllCrawlerfilestandardcolumns(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findAllCrawlerfilestandardcolumns(int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestandardcolumns", startResult, maxRows);
		return new LinkedHashSet<Crawlerfilestandardcolumn>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnByCompositeProperities(String sql,
			int startResult, int maxRows) {
		return new LinkedHashSet<Crawlerfilestandardcolumn>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> findCrawlerfilestandardcolumnsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestandardcolumn.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumns = new HashSet(
				detachedCriteria.getExecutableCriteria(session).setFirstResult(page.getSearchBeginCursor())
						.setMaxResults(page.getPaginationSize()).list());

		return crawlerfilestandardcolumns;
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnByPhysicalPrimaryKey(String name)
			throws DataAccessException {
		return findCrawlerfilestandardcolumnByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnByPhysicalPrimaryKey(String name, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestandardcolumnByPhyscialPrimaryKey", startResult, maxRows,
					name);
			return (Crawlerfilestandardcolumn) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfilestandardcolumn entity) {
		return true;
	}
}
